home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / vdigit.zip / BIN2ASM.C < prev    next >
C/C++ Source or Header  |  1989-06-24  |  3KB  |  159 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  Command line:                                                    */
  4. /*   BIN2ASM <source file> <dest file>                               */
  5. /*                                                                   */
  6. /*********************************************************************/
  7.  
  8. #include <fcntl.h>
  9. #include <io.h>
  10. #include <sys\types.h>
  11. #include <sys\stat.h>
  12. #include <stdio.h>
  13. #include <stddef.h>
  14. #include <malloc.h>
  15.  
  16. #define maxline  76
  17.  
  18. unsigned char *buffer;
  19. int s_handle,d_handle;
  20. char errmsg1[] = "File read error.";
  21. char errmsg2[] = "File write error.";
  22. char linebuffer[maxline+6] = " DB ";
  23. char divider[] = "\r\n;--------------------------------------------------\r\n\r\n";
  24.  
  25. main(argc,argv)
  26. int argc;
  27. char **argv;
  28.  
  29. {
  30. unsigned short i,fblock,byte_counter;
  31. long s_len;
  32. short linelength;
  33. char sbuf[8];
  34.  
  35. if (argc != 3)
  36.     {
  37.     puts("\nCommand line:");
  38.     puts(" BIN2ASM <source file> <dest file>\n");
  39.     exit(1);
  40.     }
  41.  
  42. argv++;
  43. s_handle = open(*argv,O_BINARY|O_RDONLY);
  44. if (s_handle == -1)
  45.     {
  46.     puts(errmsg1);
  47.     exit(1);
  48.     }
  49. argv++;
  50. d_handle = open(*argv,O_BINARY|O_RDWR|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
  51. if (d_handle == -1)
  52.     {
  53.     close(s_handle);
  54.     puts(errmsg2);
  55.     exit(1);
  56.     }
  57.  
  58. buffer = (unsigned char *)malloc(32768);
  59. if (buffer == NULL)
  60.     {
  61.     puts("Unable to allocate memory.");
  62.     close(s_handle);
  63.     close(d_handle);
  64.     exit(1);
  65.     }
  66.  
  67. s_len = filelength(s_handle);
  68. byte_counter = 0;
  69.  
  70. while (s_len > 0L)
  71.     {
  72.     if (s_len >= 32768L)
  73.         fblock = 32768;
  74.     else
  75.         fblock = (unsigned short)s_len;
  76.     
  77.     if (read(s_handle,buffer,fblock) != fblock)
  78.         {
  79.         close(s_handle);
  80.         close(d_handle);
  81.         free(buffer);
  82.         puts(errmsg1);
  83.         exit(1);
  84.         }
  85.  
  86.     s_len -= (long)fblock;
  87.     byte_counter += fblock;
  88.  
  89.     for (i=0 ; i<fblock ; i++)
  90.         {
  91.         itoa((int)buffer[i],sbuf,10);
  92.         if (strlen(linebuffer) > 4)
  93.             strcat(linebuffer,",");
  94.         strcat(linebuffer,sbuf);
  95.         if (strlen(linebuffer) >= maxline)
  96.             {
  97.             strcat(linebuffer,"\r\n");
  98.             linelength = strlen(linebuffer);
  99.             if (write(d_handle,linebuffer,linelength) != linelength)
  100.                 {
  101.                 close(s_handle);
  102.                 close(d_handle);
  103.                 free(buffer);
  104.                 puts(errmsg2);
  105.                 exit(1);
  106.                 }
  107.             linebuffer[4] = 0;
  108.             }
  109.         }
  110.  
  111.     if (byte_counter == 0)
  112.         {
  113.         if (strlen(linebuffer) > 4)
  114.             {
  115.             strcat(linebuffer,"\r\n");
  116.             linelength = strlen(linebuffer);
  117.             if (write(d_handle,linebuffer,linelength) != linelength)
  118.                 {
  119.                 close(s_handle);
  120.                 close(d_handle);
  121.                 free(buffer);
  122.                 puts(errmsg2);
  123.                 exit(1);
  124.                 }
  125.             linebuffer[4] = 0;
  126.             }
  127.  
  128.         if (write(d_handle,divider,strlen(divider)) != strlen(divider))
  129.             {
  130.             close(s_handle);
  131.             close(d_handle);
  132.             free(buffer);
  133.             puts(errmsg2);
  134.             exit(1);
  135.             }
  136.         }
  137.     }
  138.  
  139. if (strlen(linebuffer) > 4)
  140.     {
  141.     strcat(linebuffer,"\r\n");
  142.     linelength = strlen(linebuffer);
  143.     if (write(d_handle,linebuffer,linelength) != linelength)
  144.         {
  145.         close(s_handle);
  146.         close(d_handle);
  147.         free(buffer);
  148.         puts(errmsg2);
  149.         exit(1);
  150.         }
  151.     }
  152.  
  153. puts("File conversion successful.");
  154. close(s_handle);
  155. close(d_handle);
  156. free(buffer);
  157. exit(0);
  158. }
  159.